<script setup>
import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
import { onLoad, onShow } from '@dcloudio/uni-app'

import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
import DescriptionAttachment from '@/components/audit/function/descriptionAttachment.vue'
import { ref } from 'vue'
import dayjs from 'dayjs'
import optionDeptPerson from '@/store/modules/optionDeptPerson'
import { getUserProfile } from '@/api/system/user'
import { addAdministrativeApply } from '@/api/attendance'
import approvalProcessState from '@/store/modules/approvalProcess'
import { isObjectValuesNotEmpty } from '@/utils/objIsnull.js'
const title = '用章用印申请'

const entertainDateRef = ref(null)
const ItemRequisitionList = ref({
  date: '',
  users: '',
  deptName: '',
  deptId: '',
  officialSeal: '',
  printingCompanyName: '',
  filesType: '',
  printedDocuments: '',
})

const officialSealRef = ref(null)
const officialSealColumns = [['公章', '合同章', '招投标专用章']]
const officialSealConfirm = (e) => {
  ItemRequisitionList.value.officialSeal = e.value[0]
}
const officialSealFn = () => {
  officialSealRef.value.open()
}
const filesTypeRef = ref(null)
const filesTypeColumns = [['报账类', '合同类', '招投标类', '工商税务类', '其他']]
const filesTypeConfirm = (e) => {
  ItemRequisitionList.value.filesType = e.value[0]
}
const filesTypeFn = () => {
  filesTypeRef.value.open()
}

const formatter = (type, value) => {
  if (type === 'year') {
    return `${value}年`
  }
  if (type === 'month') {
    return `${value}月`
  }
  if (type === 'day') {
    return `${value}日`
  }
  if (type === 'hour') {
    return `${value}时`
  }
  if (type === 'minute') {
    return `${value}分`
  }
  return value
}

const entertainDateFn = () => {
  entertainDateRef.value.open()
}
let entertainDatePicker = dayjs()
const entertainDateConfirm = (obj) => {
  ItemRequisitionList.value.date = dayjs(obj.value).format('YYYY-MM-DD')
}

const usersFn = () => {
  uni.navigateTo({
    url: '/components/OptionDeptPerson/index?type=person',
  })
}
const withSealFn = () => {
  // uni.navigateTo({
  //   url: '/components/OptionDeptPerson/index?type=dept',
  // })
	uni.navigateTo({
	  url: '/components/OptionDeptPerson/deptName?type=dept',
	})
}

onShow(() => {
  // if (optionDeptPerson.state.optionPerson) {
  //   ItemRequisitionList.value.users = optionDeptPerson.state.optionPerson.nickName
  //   ItemRequisitionList.value.usersId = optionDeptPerson.state.optionPerson.userId
  // }
  // if (optionDeptPerson.state.optionDept) {
  //   ItemRequisitionList.value.deptName = optionDeptPerson.state.optionDept.label
  //   ItemRequisitionList.value.deptId = optionDeptPerson.state.optionDept.depId
  // }

	// 监听事件 获取选择的部门
	uni.$on('getOptionDept', (data) => {
    console.log(data,'dua')
		// console.log(data, '部门data')
		ItemRequisitionList.value.deptName = data.label
		ItemRequisitionList.value.deptId = data.id ? data.id : data.depId // id 子部门id depId部门id
	});
})

const getUserProfileFn = async () => {
  let { code, data } = await getUserProfile()
  if (code === 200) {
    userId.value = data.userId
    ItemRequisitionList.value.users = data.nickName
  }
}
const goHistoryView = () => {
  uni.navigateTo({
    url: '/components/TypeDefineModel?type=用章用印申请&apiName=行政',
  })
}
let attchment = ref([])
const getDesignDrawingsEmit = (emit) => {
  attchment.value = emit
}
const userId = ref(null)
const clickSave = async () => {
	ItemRequisitionList.value.attchment = attchment.value
	if(isObjectValuesNotEmpty(ItemRequisitionList.value)){
		let params = {
		  date: dayjs(ItemRequisitionList.value.date).format('YYYY-MM-DD hh:mm:ss'),
		  userId: userId.value,
		  deptId: ItemRequisitionList.value.deptId,
		  sealType: ItemRequisitionList.value.officialSeal,
		  otherCompanyName: ItemRequisitionList.value.printingCompanyName,
		  fileClass: ItemRequisitionList.value.filesType,
		  name: ItemRequisitionList.value.printedDocuments,
		  ruleId: 'yongzhang',
		  type: '用章用印申请',
		  attchment: JSON.stringify(attchment.value),
		  approve: approvalProcessState.state.approvalProcess,
		}
    console.log(params)
		let { code } = await addAdministrativeApply(params)
		if (code === 200) {
		  uni.showToast({
		    title: '提交成功',
		    duration: 1500,
		    icon: 'success',
		  })
		
		  setTimeout(() => {
		    uni.navigateBack()
		  }, 1000)
		}
	}else{
		uni.showToast({
			title:'请输入完整数据',
			icon:'error'
		})
	}
}

onLoad(() => {
  getUserProfileFn()
})
</script>

<template>
  <view>
    <!--    请假  -->
    <HeaderNavTabarSvg :title="title">
      <image class="bukaSvg" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView"></image>
    </HeaderNavTabarSvg>

    <view class="picker-content" @tap="entertainDateFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">用印日期</text>
        <view class="info-list-right">
          <view v-if="ItemRequisitionList.date" class="uni-input"
            >{{ ItemRequisitionList.date }}
          </view>
          <view v-else class="uni-inputs">请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <uv-datetime-picker
      ref="entertainDateRef"
      v-model="entertainDatePicker"
      :formatter="formatter"
      mode="date"
      @confirm="entertainDateConfirm"
    />

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">使用人</text>
        <view class="info-list-right">
          <view v-if="ItemRequisitionList.users" class="uni-input"
            >{{ ItemRequisitionList.users }}
          </view>
          <view v-else class="uni-inputs">请选择</view>

          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>

    <view class="picker-content" @tap="withSealFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">用印部门</text>
        <view class="info-list-right">
          <view v-if="ItemRequisitionList.deptName" class="uni-input"
            >{{ ItemRequisitionList.deptName }}
          </view>
          <view v-else class="uni-inputs">请选择</view>

          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>

    <view class="picker-content" @tap="officialSealFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">印章类型</text>
        <view class="info-list-right">
          <view v-if="ItemRequisitionList.officialSeal" class="uni-input"
            >{{ ItemRequisitionList.officialSeal }}
          </view>
          <view v-else class="uni-inputs">请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <uv-picker
      ref="officialSealRef"
      :columns="officialSealColumns"
      @confirm="officialSealConfirm"
    ></uv-picker>

    <view class="textarea-box">
      <text class="info-list-left isrequired">用印公司名称</text>
      <textarea
        v-model="ItemRequisitionList.printingCompanyName"
        class="textinfo-list"
        placeholder="请输入"
      ></textarea>
    </view>

    <view class="picker-content" @tap="filesTypeFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">文件类型</text>
        <view class="info-list-right">
          <view v-if="ItemRequisitionList.filesType" class="uni-input"
            >{{ ItemRequisitionList.filesType }}
          </view>
          <view v-else class="uni-inputs">请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <uv-picker
      ref="filesTypeRef"
      :columns="filesTypeColumns"
      @confirm="filesTypeConfirm"
    ></uv-picker>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">用印文件名称</text>
        <view class="info-list-right">
          <view class="info-content">
            <input
              v-model="ItemRequisitionList.printedDocuments"
              adjust-position
              class="uni-input"
              placeholder="请输入用印文件名称"
              type="text"
            />
          </view>
        </view>
      </view>
    </view>

    <!--    说明附件-->
    <DescriptionAttachment :isrequired="true" @getDesignDrawingsEmit="getDesignDrawingsEmit" />
    <!--    审批流程-->
    <AuditApplicationSteps
      :fileType="ItemRequisitionList.filesType"
      :ruleId="'yongzhang'"
      :deptId="ItemRequisitionList.deptId"
      :sealType="ItemRequisitionList.officialSeal"
    />
    <!--    底部占位符-->
    <view class="blank"></view>
    <!--    保存-->
    <view class="buttonContainer">
      <button class="buttonClass" @click="clickSave">提交</button>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@import '@/static/style/index.scss';

.bukaSvg {
  width: 32rpx;
  height: 32rpx;
}

.buttonContainer {
  position: fixed;
  bottom: 0;
  display: flex;
  justify-content: center;
  width: 100%;
  align-items: center;
  padding: 16rpx;
  background-color: #fff;
}

.buttonClass {
  display: flex;
  width: 686rpx;
  height: 72rpx;
  justify-content: center;
  align-items: center;
  background: #165dff;
  border-radius: 16rpx;
  color: #fff;
  font-size: 32rpx;
  //margin-top: 40rpx;
  overflow: hidden;
}

.uni-inputs {
  color: #86909c !important;
}

.uni-input {
  text-align: right;
  font-size: 28rpx;
}

.blank {
  height: calc(72rpx + 32rpx + 20rpx);
}

.textarea-box {
  background: #fff;
  padding: 20rpx;
  margin-top: 20rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .textinfo-list {
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
  }
}

.bukabanci {
  .info-content {
    color: #86909c !important;
  }

  .info-list-left {
    // @include font-color-auxiliary;
    color: #86909c !important;
  }
}

.textarea-box {
  background: #fff;
  padding: 20rpx;
  margin-top: 20rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .textinfo-list {
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
  }
}

.popup-content {
  background: #f7f8fa;

  .header-box {
    padding: 30rpx 20rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;

    p {
      color: #1d2129;
      font-size: 34rpx;
      font-weight: 500;
    }

    .module-image-ba {
      border-radius: 32rpx;
      background: #f2f3f5;
      width: 45rpx;
      height: 45rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      image {
        width: 32rpx;
        height: 32rpx;
      }
    }
  }

  .card-list {
    display: flex;
    align-items: center;
    flex-direction: column;
    padding: 20rpx 30rpx;
    box-sizing: border-box;

    .list {
      box-shadow: 0 4rpx 16rpx 0 #0000000d;
      box-sizing: border-box;
      width: 100%;
      background: #fff;
      padding: 16rpx 24rpx;
      border-radius: 8rpx;
      margin-bottom: 10rpx;

      h4 {
        color: #1d2129;
        font-size: 32rpx;
        font-weight: 400;
      }

      p {
        color: #86909c;
        font-size: 24rpx;
        font-weight: 400;
        margin-top: 10rpx;
      }
    }
  }
}

.detail-itemRequisition {
  .detail-item {
    padding: 10rpx 30rpx;
    @include flex($align: center, $justify: space-between);

    .detail-utils {
      @include flex($align: center, $justify: space-between);
      width: 150rpx;
      color: #165dff;
    }
  }

  .text-info {
    color: #86909c;
    padding: 20rpx 30rpx;
    display: inline-block;
  }
}

.measurement-info-list {
  @include flex($align: center, $justify: space-between);
  padding: $spacing-row-base;
  background: #fff;
  margin-top: 16rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .info-list-right {
    @include flex($align: center);

    .info-content {
      @include textStyleBase;
      font-size: 34rpx;
    }

    .iconfont {
      margin-left: 16rpx;
      // @include font-color-auxiliary;
      color: $uni-fc-fu-lin;
    }
  }

  .unit {
    // @include font-color-auxiliary;
    color: $uni-fc-fu-lin;
  }
}
</style>
